﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace StockEngine
{
    public class ReadCSV
    {
        private WebClient wC = new WebClient();
        private List<StockInfo> myStockInfoList;
        private ListStorage myListStorage;

        public ReadCSV(ListStorage myListStorage)
        {
            this.myListStorage = myListStorage;
        }

        public Dictionary<string, List<StockInfo>> ReadCSV_(string stockName)
        {
            if (myListStorage.GetStockInfoDictionary().ContainsKey(stockName))
                return myListStorage.GetStockInfoDictionary();
            try
            {
                myStockInfoList = new List<StockInfo>();
                SplitArrayIntoObject(getFileContent(getCSVURL(stockName)));
                myListStorage.AddToStockInfoDicationary(stockName, myStockInfoList);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in ReadCSV Constructor: " + e.ToString());
                throw new System.Exception("Error in ReadCSV Constructor");
            }

            return myListStorage.GetStockInfoDictionary();
        }

        public string getCSVURL(string stockValue)
        {
            try
            {
                string firstURL = "http://finance.yahoo.com/q/hp?s=" + stockValue + "+Historical+Prices";
                string secondURL = wC.DownloadString(firstURL);
                string subString = secondURL.Substring(0, secondURL.IndexOf("Download to Spreadsheet"));
                subString = subString.Substring(subString.LastIndexOf("href=\"") + 6, subString.LastIndexOf("\"><img") - subString.LastIndexOf("href=\"") - 6);
                return subString;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in getCSVURL Constructor: " + e.ToString());
                throw new System.Exception("Error in getCSVURL Constructor");
            }
        }

        public string[] getFileContent(string fileName)
        {
            try
            {
                string csvdata = wC.DownloadString(fileName);
                string[] csvdataarray = csvdata.Split('\n');
                return csvdataarray;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in getFileContent Constructor: " + e.ToString());
                throw new System.Exception("Error in getFileContent Constructor");
            }
        }

        public void SplitArrayIntoObject(string[] CSVarray)
        {
            try
            {
                for (int i = 1; i < CSVarray.Length; i++)
                {
                    if (CSVarray[i].Length > 0)
                    {
                        //date,open,high,low,close,volume,adj close
                        string[] rowData;
                        rowData = CSVarray[i].Split(',');
                        myStockInfoList.Add(new StockInfo(Convert.ToDateTime(rowData[0]),
                            double.Parse(rowData[1].Replace(".", ",")), double.Parse(rowData[2].Replace(".", ",")),
                            double.Parse(rowData[3].Replace(".", ",")), double.Parse(rowData[4].Replace(".", ",")),
                            int.Parse(rowData[5].Replace(".", ",")), double.Parse(rowData[6].Replace(".", ","))));
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in SplitArrayIntoObject Constructor: " + e.ToString());
                throw new System.Exception("Error in SplitArrayIntoObject Constructor");
            }
        }
    }
}
